import axiosInstance from "./ajax"
import type { ResDataType } from "./ajax"

// 获取单个问卷信息
export async function getQuestionService(id: string): Promise<ResDataType> {
    const url = `/api/question/${id}`
    // 响应拦截器处理了结果，返回的data就是需要的数据，不需要再.data了
    const data = (await axiosInstance.get(url)) as ResDataType
    return data
}

// 创建问卷
export async function createQuestionService(): Promise<ResDataType> {
    const url = `/api/question`
    const data = (await axiosInstance.post(url)) as ResDataType
    return data
}

type SearchOption = {
    keyword: string
    // 还有其他配置
    isStar: boolean // 在星标页面 只查询带星的
    isDeleted: boolean // 在回收站页面 只查询浅删除的
    page: number
    pageSize: number
}
// 获取（查询）问卷列表
export async function getQuestionListService(params: Partial<SearchOption> = {}): Promise<ResDataType> {
    const url = "/api/question"
    // 传入params配置项  比如params={a:1,b:2}  url就是："/api/question?a=1&b=2"
    const data = (await axiosInstance.get(url, { params: params })) as ResDataType
    return data
}

// 更新问卷信息
export async function updateQuestionService(id: string, opt: { [key: string]: any }): Promise<ResDataType> {
    const url = `/api/question/${id}`
    const data = (await axiosInstance.patch(url, opt)) as ResDataType

    return data
}

// 复制问卷
export async function duplicateQuestionService(id: string): Promise<ResDataType> {
    const url = `/api/question/duplicate/${id}`
    const data = (await axiosInstance.post(url)) as ResDataType
    return data
}

// 批量彻底删除（ids是要删除问卷的id列表）
export async function deleteQuestionService(ids: string[]): Promise<ResDataType> {
    const url = "/api/question/"
    const data = (await axiosInstance.delete(url, { data: { ...ids } })) as ResDataType
    return data
}
